package com.abel.web.validator;

import java.text.SimpleDateFormat;

import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import com.abel.domain.WorkExperience;
import com.abel.web.view.WorkExperienceViewBean;


public class WorkExperienceViewValidator implements Validator{

	@SuppressWarnings("rawtypes")
	@Override
	public boolean supports(Class clazz) {
		return WorkExperienceViewBean.class.equals(clazz);
	}

	@Override
	public void validate(Object obj, Errors errors) {

		WorkExperienceViewBean viewBean = (WorkExperienceViewBean)obj;
		
		if (viewBean.getEditId()==0){
//			if (viewBean.getWrkStartDate() == null 
//					|| viewBean.getWrkStartDate().equals("") 
//					|| !viewBean.getWrkStartDate().matches("\\d{2}[-|/]\\d{4}")) {
//				errors.rejectValue("wrkStartDate", "error.null",null,"Invalid Work Start Date. Valid format is mm/yyyy");
//			}
			if (viewBean.getWrkStartDate() == null 
					|| viewBean.getWrkStartDate().equals("")) {
				errors.rejectValue("wrkStartDate", "error.null",null,"Work Start Date cannot be empty");
			}
			
			if (viewBean.getWrkStartDate() != null 
					&& !viewBean.getWrkStartDate().equals("")) {
				try {
					
					if (!viewBean.getWrkStartDate().matches("(0?[1-9]|1[012])/(0?[1-9]|[12][0-9]|3[01])/((19|20)\\d\\d)")) {
						errors.rejectValue("wrkStartDate", "error.null",null,"Invalid Work Start Date. Valid format is mm/dd/yyyy");
					}
					else {
						SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy");
					    df.setLenient(false);
					    df.parse(viewBean.getWrkStartDate());
					}
				}
				catch (Exception e) {
					errors.rejectValue("wrkStartDate", "error.null",null,"Invalid Work Start Date. Valid format is mm/dd/yyyy");
				}
			}
			
//			if (viewBean.getWrkEndDate() != null 
//					&& !viewBean.getWrkEndDate().equals("") 
//					&& !viewBean.getWrkEndDate().matches("\\d{2}[-|/]\\d{4}")) {
//				errors.rejectValue("wrkEndDate", "error.null",null,"Invalid Work End Date. Valid format is mm/yyyy");
//			}

			if (viewBean.getWrkEndDate() != null 
					&& !viewBean.getWrkEndDate().equals("")) {
				try {
					
					if (!viewBean.getWrkEndDate().matches("(0?[1-9]|1[012])/(0?[1-9]|[12][0-9]|3[01])/((19|20)\\d\\d)")) {
						errors.rejectValue("wrkStartDate", "error.null",null,"Invalid Work End Date. Valid format is mm/dd/yyyy");
					}
					else {
						SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy");
					    df.setLenient(false);
					    df.parse(viewBean.getWrkEndDate());
					}
				}
				catch (Exception e) {
					errors.rejectValue("wrkEndDate", "error.null",null,"Invalid Work End Date. Valid format is mm/dd/yyyy");
				}
			}

			
			if (viewBean.getWrkCompanyName() == null
					|| viewBean.getWrkCompanyName().equals("")) {
				errors.rejectValue("wrkCompanyName", "error.null",null,"Company Name is a required field");
			}

			if (viewBean.getWrkAddrLine1() == null
					|| viewBean.getWrkAddrLine1().equals("")) {
				errors.rejectValue("wrkAddrLine1", "error.null",null,"Address Line1 is a required field");
			}

			if (viewBean.getWrkCity() == null
					|| viewBean.getWrkCity().equals("")) {
				errors.rejectValue("wrkCity", "error.null",null,"City is a required field");
			}
			
			if (viewBean.getWrkState() == null
					|| viewBean.getWrkState().equals("")) {
				errors.rejectValue("wrkState", "error.null",null,"State is a required field");
			}

			if (viewBean.getWrkZip() == null
					|| viewBean.getWrkZip().equals("")) {
				errors.rejectValue("wrkZip", "error.null",null,"Zip Code is a required field");
			}
			
			if (viewBean.getWrkBusinessType() == null
					|| viewBean.getWrkBusinessType().equals("")) {
				errors.rejectValue("wrkBusinessType", "error.null",null,"Business type is a required field");
			}

			if (viewBean.getWrkPosition() == null
					|| viewBean.getWrkPosition().equals("")) {
				errors.rejectValue("wrkPosition", "error.null",null,"Position is a required field");
			}
		
			if (viewBean.getWrkResponsibility() == null
					|| viewBean.getWrkResponsibility().equals("")) {
				errors.rejectValue("wrkResponsibility", "error.null",null,"Work responsibility is a required field");
			}

			if (viewBean.getWrkSupervisorName() == null
					|| viewBean.getWrkSupervisorName().equals("")) {
				errors.rejectValue("wrkSupervisorName", "error.null",null,"Supervisor name is a required field");
			}
		}
		else {
			
			for(WorkExperience work : viewBean.getWorkList()) {
				if (viewBean.getEditId() == work.getWrkHistId())
				{
					if (work.getWrkStartDate() == null 
							|| work.getWrkStartDate().equals("") 
							|| !work.getWrkStartDate().matches("\\d{2}[-|/]\\d{4}")) {
						errors.rejectValue("wrkStartDate", "error.null",null,"Invalid Work Start Date. Valid format is mm/yyyy");
						}
					
					if (work.getWrkEndDate() != null 
							&& !work.getWrkEndDate().equals("") 
							&& !work.getWrkEndDate().matches("\\d{2}[-|/]\\d{4}")) {
						errors.rejectValue("wrkEndDate", "error.null",null,"Invalid Work End Date. Valid format is mm/yyyy");
						}
					
					if (work.getWrkCompanyName() == null
							|| work.getWrkCompanyName().equals("")) {
						errors.rejectValue("wrkCompanyName", "error.null",null,"Company Name is a required field");
					}
		
					if (work.getWrkAddrLine1() == null
							|| work.getWrkAddrLine1().equals("")) {
						errors.rejectValue("wrkAddrLine1", "error.null",null,"Adress Line1 is a required field");
					}

					if (work.getWrkCity() == null
							|| work.getWrkCity().equals("")) {
						errors.rejectValue("wrkCity", "error.null",null,"City is a required field");
					}
					
					if (work.getWrkState() == null
							|| work.getWrkState().equals("")) {
						errors.rejectValue("wrkState", "error.null",null,"State is a required field");
					}

					if (work.getWrkZip() == null
							|| work.getWrkZip().equals("")) {
						errors.rejectValue("wrkZip", "error.null",null,"Zip Code is a required field");
					}
					
					if (work.getWrkBusinessType() == null
							|| work.getWrkBusinessType().equals("")) {
						errors.rejectValue("wrkBusinessType", "error.null",null,"Business type is a required field");
					}
					
					if (work.getWrkPosition() == null
							|| work.getWrkPosition().equals("")) {
						errors.rejectValue("wrkPosition", "error.null",null,"Position is a required field");
					}
					
					if (work.getWrkResponsibility() == null
							|| work.getWrkResponsibility().equals("")) {
						errors.rejectValue("wrkResponsibility", "error.null",null,"Work responsibility is a required field");
					}

					if (work.getWrkSupervisorName() == null
							|| work.getWrkSupervisorName().equals("")) {
						errors.rejectValue("wrkSupervisorName", "error.null",null,"Supervisor name is a required field");
					}

					if (work.getWrkGrossHourlyPay() == 0 
							&& work.getWrkGrossAnnualPay() == 0) {
						errors.rejectValue("getWrkGrossAnnualPay", "error.null",null,"Both pay fields cannot be zero");
					}
				}
			}
		}
	}
}
